package com.sxygsj.android.personnelscreen.main;

import android.content.Context;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.os.Environment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.bumptech.glide.Glide;
import com.sxygsj.android.personnelscreen.R;
import com.tencent.mmkv.MMKV;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class EmptyView extends ConstraintLayout {

    private VideoView videoView;
    private ImageView imageView;
    private List<String> videoArrayList = new ArrayList<>();
    private String playVideoName = "video_1";

    public EmptyView(@NonNull Context context) {
        super(context);
        init(context);
    }


    public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_advertisement, this);
        videoView = findViewById(R.id.view_advertisement_vv_advertisement);
        imageView=findViewById(R.id.view_advertisement_iv_advertisement);
        videoArrayList.add(
                Environment.getExternalStorageDirectory()+"/com.sxygsj.android.personnelscreen.advertisement/video_1.mp4"
        );
        videoArrayList.add(
                Environment.getExternalStorageDirectory()+"/com.sxygsj.android.personnelscreen.advertisement/video_2.mp4");
        videoArrayList.add(
                Environment.getExternalStorageDirectory()+"/com.sxygsj.android.personnelscreen.advertisement/video_3.mp4");

        videoView.setMediaController(new MediaController(context));
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                if ("video_1".equals(playVideoName)) {
                    videoView.setVideoPath(videoArrayList.get(1));
                    playVideoName = "video_2";
                } else if ("video_2".equals(playVideoName)) {
                    videoView.setVideoPath(videoArrayList.get(2));
                    playVideoName = "video_3";
                } else if ("video_3".equals(playVideoName)) {
                    videoView.setVideoPath(videoArrayList.get(0));
                    playVideoName = "video_1";
                }
                videoView.start();
            }
        });


        MMKV mmkv = MMKV.mmkvWithID("advertisementFile");
        String filePath = mmkv.decodeString("file", "");
        if (TextUtils.isEmpty(filePath)) {
            filePath = Environment.getExternalStorageDirectory()+"/com.sxygsj.android.personnelscreen.advertisement/video_1.mp4";
            File video1 = new File(filePath);
            if (video1.exists()) {
                playVideoName = video1.getName();
                videoView.setVisibility(VISIBLE);
                imageView.setVisibility(GONE);
                videoView.setVideoPath(filePath);
                videoView.start();
            } else {
                videoView.setVisibility(GONE);
                imageView.setVisibility(VISIBLE);
              Glide.with(this).load(R.drawable.main_recycler_item_bg).into(imageView);
            }

        } else {
            File file = new File(filePath);
            playVideoName = file.getName();
            String fileType= playVideoName.substring(playVideoName.lastIndexOf(".") + 1, playVideoName.length()).toLowerCase();
            if (fileType .equals("jpg")  || fileType .equals("png")) {
                videoView.setVisibility(GONE);
                imageView.setVisibility(VISIBLE);
                Glide.with(this).load(file).into(imageView);
            } else if (fileType .equals("mp4")) {
                videoView.setVisibility(VISIBLE);
                imageView.setVisibility(GONE);
                videoView.setVideoPath(filePath);
            }

        }
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
        if (gainFocus) {
            if (videoView.getVisibility() == View.VISIBLE) {
                videoView.start();
            }

        } else {
            if (videoView.getVisibility() == View.VISIBLE) {
                videoView.pause();
            }
        }
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        if (visibility==VISIBLE){
            if (videoView.getVisibility() == View.VISIBLE){
                videoView.start();
            }
        }else {
            if (videoView.getVisibility() == View.VISIBLE) {
                videoView.pause();
            }
        }
    }
}
